﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Wicture.DbRESTFul.SQL;
using Wicture.EPC.Service.V1.Models;

namespace Wicture.EPC.Service.V1.Resources
{
    public static class ResourceHelper
    {
        private static Lazy<Dictionary<string, List<Feature_Nissan>>> nissanFeatureDict = new Lazy<Dictionary<string,  List<Feature_Nissan>>>(LoadNissanFeatures);
        private static Lazy<Dictionary<string, string>> moparFeatureDict = new Lazy<Dictionary<string, string>>(LoadMoparFeatures);

        private static Dictionary<string, string> LoadMoparFeatures()
        {
            var repository = new DbRESTFulRepository();
            repository.ConnectionManager?.ProcessGateway(null, JObject.FromObject(new { brandCode = "jeep" }), null);
            IEnumerable<Feature> list = repository.Invoke<Feature>(CSIs.List_Feature, null);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            list.ForEach(x => dic.Add(x.featureCode, x.featureName));
            return dic;
        }

        public static Dictionary<string, List<Feature_Nissan>> NissanFeatureDict { get { return nissanFeatureDict.Value; } }
        public static Dictionary<string, string> MoparFeatureDict { get { return moparFeatureDict.Value; } }

        private static Lazy<Dictionary<string, List<Feature_Nissan_Combine>>> nissanFeatureCombineDict = new Lazy<Dictionary<string, List<Feature_Nissan_Combine>>>(LoadNissanFeatureCombines);
        public static Dictionary<string, List<Feature_Nissan_Combine>> NissanFeatureCombineDict { get { return nissanFeatureCombineDict.Value; } }

        private static Dictionary<string, List<Feature_Nissan>> LoadNissanFeatures()
        {
            var repository = new DbRESTFulRepository();
            repository.ConnectionManager?.ProcessGateway(null, JObject.FromObject(new { brandCode =  "nissan" }), null);
            IEnumerable<Feature_Nissan> list = repository.Invoke<Feature_Nissan>(CSIs.List_Nissan_Feature, null);
            return list.GroupBy(q => q.modelCode).ToDictionary(k => k.Key, v => v.ToList());
        }

        private static Dictionary<string, List<Feature_Nissan_Combine>> LoadNissanFeatureCombines()
        {
            var repository = new DbRESTFulRepository();
            repository.ConnectionManager?.ProcessGateway(null, JObject.FromObject(new { brandCode = "nissan" }), null);
            IEnumerable<Feature_Nissan_Combine> list = repository.Invoke<Feature_Nissan_Combine>(CSIs.List_Nissan_Feature_Combine, null);
            return list.GroupBy(q => q.modelCode).ToDictionary(k => k.Key, v => v.ToList());
        }

        #region toyota

        private static Lazy<Dictionary<string, List<Feature_Toyota>>> toyotaFeatureDict = new Lazy<Dictionary<string, List<Feature_Toyota>>>(LoadToyotaFeatures);

        public static Dictionary<string, List<Feature_Toyota>> ToyotaFeatureDict { get { return toyotaFeatureDict.Value; } }

        private static Dictionary<string, List<Feature_Toyota>> LoadToyotaFeatures()
        {
            var repository = new DbRESTFulRepository();
            repository.ConnectionManager?.ProcessGateway(null, JObject.FromObject(new { brandCode = "toyota" }), null);
            IEnumerable<Feature_Toyota> list = repository.Invoke<Feature_Toyota>(CSIs.List_Toyota_Feature, null);
            return list.GroupBy(q => q.modelCode).ToDictionary(k => k.Key, v => v.ToList());
        }
        #endregion

        #region sgm
        private static Lazy<Dictionary<string, string>> sgmFeatureDict = new Lazy<Dictionary<string, string>>(LoadSgmFeatures);
        public static Dictionary<string, string> SgmFeatureDict { get { return sgmFeatureDict.Value; } }
        private static Dictionary<string, string> LoadSgmFeatures()
        {
            var repository = new DbRESTFulRepository();
            repository.ConnectionManager?.ProcessGateway(null, JObject.FromObject(new { brandCode = "buick" }), null);
            IEnumerable<dynamic> list = repository.Invoke<dynamic>(CSIs.List_Feature, null);
            return list.GroupBy(q => q.featureCode).ToDictionary(k => (string)k.Key, v => (string)v.First()?.featureName);
        }
        #endregion
    }
}
